/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.admin;

import dp1.titandevelop.titano.bean.Estado;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Empleado;
import dp1.titandevelop.titano.persistent.TipoEmpleado;
import dp1.titandevelop.titano.service.EmpleadoService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.view.LoginT;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author chrestean
 */
public class ModulosT_ADM_AEEdit extends javax.swing.JInternalFrame {

    private Empleado aux = new Empleado();
    private List<TipoEmpleado> tipos;
    private int idEmp;

    private void addList(List<TipoEmpleado> lista, int estado) {

        for (int i = 0; i < lista.size(); i++) {
            this.comboTipoEmp.addItem(lista.get(i).getDescripcion());
        }

    }

    private int findIndex(Empleado a, List<TipoEmpleado> lista) {
        int i = 0;
        int idTipo = a.getToTipoEmpleado().getIdtipoempleado();
        while (i < lista.size()) {
            if (idTipo == lista.get(i).getIdtipoempleado()) {
                break;
            }
            i++;
        }
        return (i + 1);
    }

    public ModulosT_ADM_AEEdit(Empleado emp) {
        initComponents();
        idEmp = emp.getIdempleado();


        //Cbox con todas los tipos
        EmpleadoService serv = new EmpleadoService();
        Estado est = new Estado();
       
        tipos = serv.buscarTEmpSinOperario();
        addList(tipos, emp.getEstado());
        
        int index = findIndex(emp, tipos);

        this.textNombre.setText(emp.getNombres());
        this.textApPaterno.setText(emp.getApellidop());
        this.textApMaterno.setText(emp.getApellidom());
        this.textFono.setText(emp.getTelefono());
        this.textCel.setText(emp.getCelular());
        this.textCorreo.setText(emp.getEmail());
        this.dpFechaIn.setDate(emp.getFechanacimiento());
        this.comboTipoEmp.setSelectedIndex(index);

        ValidacionService val = new ValidacionService();
        val.SNumeros(this.textCel);
        val.SNumeros(this.textFono);
        val.Longitud(textCel, 9);
        val.Longitud(textFono, 7);
        val.SLetras(textNombre);
        val.SLetras(textApPaterno);
        val.SLetras(textApMaterno);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonAtras = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        nombre = new javax.swing.JLabel();
        apPaterno = new javax.swing.JLabel();
        apMaterno = new javax.swing.JLabel();
        fechaNac = new javax.swing.JLabel();
        telefono = new javax.swing.JLabel();
        celular = new javax.swing.JLabel();
        correo = new javax.swing.JLabel();
        textNombre = new javax.swing.JTextField();
        textApPaterno = new javax.swing.JTextField();
        textApMaterno = new javax.swing.JTextField();
        textFono = new javax.swing.JTextField();
        textCel = new javax.swing.JTextField();
        textCorreo = new javax.swing.JTextField();
        comboTipoEmp = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        dpFechaIn = new org.jdesktop.swingx.JXDatePicker();

        setResizable(true);
        setTitle("Editar Empleado");
        setMaximumSize(new java.awt.Dimension(620, 320));
        setMinimumSize(new java.awt.Dimension(620, 320));
        setPreferredSize(new java.awt.Dimension(620, 320));

        botonAtras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        botonAtras.setToolTipText("Salir");
        botonAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAtrasActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        botonGuardar.setToolTipText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        nombre.setText("Nombre");

        apPaterno.setText("Ap. Paterno");

        apMaterno.setText("Ap. Materno");

        fechaNac.setText("Fecha de Nacimiento");

        telefono.setText("Telefono");

        celular.setText("Celular");

        correo.setText("E-mail");

        textNombre.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        textApPaterno.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        textApMaterno.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        textFono.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        textCel.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        textCorreo.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        comboTipoEmp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        comboTipoEmp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboTipoEmpActionPerformed(evt);
            }
        });

        jLabel3.setText("Tipo Empleado");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(fechaNac)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(textApPaterno)
                            .addComponent(textNombre)
                            .addComponent(textApMaterno)
                            .addComponent(dpFechaIn, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(celular)
                            .addComponent(correo)
                            .addComponent(telefono)
                            .addComponent(jLabel3)))
                    .addComponent(nombre)
                    .addComponent(apPaterno)
                    .addComponent(apMaterno))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textFono, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboTipoEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nombre)
                            .addComponent(textNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(apPaterno)
                            .addComponent(textApPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(apMaterno)
                            .addComponent(textApMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fechaNac)
                            .addComponent(dpFechaIn, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(telefono)
                            .addComponent(textFono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(celular)
                            .addComponent(textCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(correo)
                            .addComponent(textCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(comboTipoEmp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(15, 15, 15))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botonGuardar)
                        .addGap(1, 1, 1)
                        .addComponent(botonAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonAtras)
                    .addComponent(botonGuardar))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
//ID 61 EditaRGuardar/Eliminar Empleado
        ValidacionService val = new ValidacionService();
        Mensajes mensajes = new Mensajes();
        int i = 0;
        int num = 0;
        for (; i < LoginT.permisosxperfil.size(); i++) {
            if (61 == (Integer) LoginT.idspermisosxperfil.get(i)) { 
              if ((val.esVacio(this.textApMaterno)) || (val.esVacio(this.textApPaterno)) || (val.esVacio(this.textCel)) || (val.esVacio(this.textCorreo))
                   || (val.esFechaVacia(this.dpFechaIn)) || (val.esVacio(this.textNombre)) || (val.esVacio(this.textFono))) {
                JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getCompletarCampos(), "Aviso", JOptionPane.WARNING_MESSAGE);

                } else {
                  if (val.validaCorreo(this.textCorreo)){
                  if (val.LongitudExacta(textCel.getText(), 9) && val.LongitudExacta(textFono.getText(), 7)){
                    
                      switch (val.fechaNacimiento(this.dpFechaIn,18)){
                        case 1: 
                            JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getFechaposterior(), "Aviso", JOptionPane.WARNING_MESSAGE);
                            break;
                        case 2: 
                            JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getFechamayor(), "Aviso", JOptionPane.WARNING_MESSAGE);
                            break;
                        case 3:   
                            
                            try {
                                EmpleadoService serv = new EmpleadoService();
                                String nombres, apepat, apemat, direcc, dni, tlf, cel, mail;
                                Date fecha;

                                nombres = this.textNombre.getText();
                                apepat = this.textApPaterno.getText();
                                apemat = this.textApMaterno.getText();
                                tlf = this.textFono.getText();
                                cel = this.textCel.getText();
                                mail = this.textCorreo.getText();
                                fecha = this.dpFechaIn.getDate();

                                serv.editarEmpleado(idEmp, tipos.get(comboTipoEmp.getSelectedIndex() - 1).getIdtipoempleado(),  fecha, nombres, apepat, apemat,tlf, cel, mail);

                                JOptionPane.showInternalMessageDialog(this.getRootPane(), mensajes.getEditado(), "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                                this.dispose();
                            } catch (Exception e) {

                                JOptionPane.showInternalMessageDialog(this.getRootPane(),mensajes.getError(),"Error",JOptionPane.ERROR_MESSAGE);
                                  }

                            
                            break;
                            
                            }
                    }else {
                            
                            if (val.LongitudExacta(textCel.getText(), 9)){
                            }else{
                                JOptionPane.showInternalMessageDialog(this.getRootPane(), "El celular debe tener 9 digitos ", "Aviso", JOptionPane.WARNING_MESSAGE);
                            }
                            if (val.LongitudExacta(textFono.getText(), 7)){
                            }  else{
                                JOptionPane.showInternalMessageDialog(this.getRootPane(), "El telefono debe tener 7 digitos ", "Aviso", JOptionPane.WARNING_MESSAGE);
                            }
                    }
               }else{
                JOptionPane.showInternalMessageDialog(this.getRootPane(), "Correo con formato erroneo ", "Aviso", JOptionPane.WARNING_MESSAGE);
                  } 
              }
              break;
            }
            
        }
        if (i >= LoginT.permisosxperfil.size()) {
            JOptionPane.showMessageDialog(this, "No puedes proceder, al parecer careces de permisos.",
                    "No posee permisos",
                    JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAtrasActionPerformed
        ModulosT_ADM.menuEmp.RefreshTable();
        this.dispose();
    }//GEN-LAST:event_botonAtrasActionPerformed

    private void comboTipoEmpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboTipoEmpActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboTipoEmpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel apMaterno;
    private javax.swing.JLabel apPaterno;
    private javax.swing.JButton botonAtras;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JLabel celular;
    private javax.swing.JComboBox comboTipoEmp;
    private javax.swing.JLabel correo;
    private org.jdesktop.swingx.JXDatePicker dpFechaIn;
    private javax.swing.JLabel fechaNac;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel nombre;
    private javax.swing.JLabel telefono;
    private javax.swing.JTextField textApMaterno;
    private javax.swing.JTextField textApPaterno;
    private javax.swing.JTextField textCel;
    private javax.swing.JTextField textCorreo;
    private javax.swing.JTextField textFono;
    private javax.swing.JTextField textNombre;
    // End of variables declaration//GEN-END:variables
}
